<script setup>
import { ref, reactive, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import Chart from "@/components/Chart/index.vue";
import AppHeader from "@/components/app_header.vue";
import { useRouter } from "vue-router";
import { dappOrderApi, assetStatesControllerApi } from "@/api/public";
import { fromWeiToFloat, encryptAddress } from "@/utils";
import { useConfigStore } from "@/store/modules/config";
const useConfig = useConfigStore();

const { locale, t } = useI18n();
const isShow = ref(false);
const loading = ref(false);
const finished = ref(false);
const list = reactive([]);
const tabs = reactive([t("exchange.buyIn"), t("exchange.sellOut")]);
const router = useRouter();
// 去购买
const ToBuy = address => {
  router.push({
    name: "Exchange",
    query: { address }
  });
};
const ToBlack = () => {
  router.push("home");
};
const CreateOrder = () => {
  router.push("createOrder");
};
const MineOrder = () => {
  router.push("exchangeRecord");
};

/// ======================bigv

// CBT价格走势
const assetStatesControllerList = ref([]);
const assetStatesControllerFn = async () => {
  const res = await assetStatesControllerApi();

  assetStatesControllerList.value = res.map(v => {
    return {
      time: v.created_at,
      vol: fromWeiToFloat(v.price)
    };
  });
  console.log("assetStatesControllerList", assetStatesControllerList);
};

// 订单排序

const sortVal = ref(0);

const sortOrder = async () => {
  sortVal.value = sortVal.value === 1 ? 0 : 1;
  dappOrderFn();
};

// 买卖订单列表
const dappOrderList = ref([]);
const dappOrderFn = async () => {
  const params = {
    page: 0,
    size: 50,
    sort: sortVal.value
  };
  const res = await dappOrderApi(params);
  dappOrderList.value = res;
};

onMounted(() => {
  dappOrderFn();
  assetStatesControllerFn();
});
</script>

<template>
  <div class="home div-container">
    <!-- header--start-->
    <div class="div-header">
      <div class="div-left" @click.stop="ToBlack">
        <img src="@/assets/icon-black.png" alt="" />
      </div>
      <span class="title">
        <img src="@/assets/icon-logo.png" width="40" alt="" />
      </span>
      <div class="div-right">...</div>
    </div>
    <!-- header--start-->
    <AppHeader />

    <div v-if="assetStatesControllerList.length > 1" class="div-chart">
      <Chart :data="assetStatesControllerList" />
    </div>

    <div class="div-record">
      <div class="div-record-title">
        {{ $t("transaction.sellerOrder") }}
        <img
          src="./../../assets/icon-transaction-sort.png"
          width="19"
          height="26"
          alt=""
          :style="{
            transform: `${sortVal === 0 ? 'rotateX(180deg)' : 'rotateX(0deg)'}`
          }"
          @click="sortOrder"
        />
      </div>

      <div class="div-list">
        <div v-for="(v, i) in dappOrderList" :key="i" class="div-list-item">
          <div class="div-top">
            <!--                <span class="div-status div-success">已成交</span>-->
            <span>{{ encryptAddress(v.form_address) }}</span>
          </div>

          <p class="div-price">{{ $t("transaction.price") }}(USDT)</p>
          <p class="div-amount">{{ fromWeiToFloat(v.price) }}</p>
          <p class="pay-detail">
            <span class="sub-desc">{{ $t("transaction.quantity") }}：</span
            >{{ fromWeiToFloat(v.deposit_price) }} {{ useConfig.name }}
          </p>
          <p class="pay-detail">
            <span class="sub-desc">{{ $t("transaction.limit") }}：</span
            >{{
              fromWeiToFloat(parseFloat(v.deposit_price)) -
              fromWeiToFloat(parseFloat(v.deal_price))
            }}
            {{ useConfig.name }}
          </p>

          <div class="div-btn" @click.stop="() => ToBuy(v.form_address)">
            {{ $t("transaction.buy") }}
          </div>
        </div>

        <div v-show="!dappOrderList.length" class="div-list-empty">
          <img src="./../../assets/icon-revenue-empty.png" width="150" alt="" />
          <p>{{ $t("transaction.tableEmpty") }}</p>
        </div>
      </div>
    </div>

    <div class="div-operate">
      <div class="div-content">
        <div class="div-btn btn-create" @click.stop="CreateOrder">
          {{ $t("transaction.createOrder") }}
        </div>
        <div class="div-btn btn-order" @click.stop="MineOrder">
          {{ $t("transaction.myOrder") }}
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
@import "./../../styles/mixin.less";
p {
  margin: 0px;
}
.home {
  font-size: 14px;
  color: #ffffff;
  padding-bottom: 20px;
}
.div-wallet {
  height: 44px;
  padding: 0 14px;
  display: flex;
  font-size: 14px;
  justify-content: space-between;
  & > div {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  .wallet {
    padding-left: 10px;
    padding-right: 10px;
    border-radius: 20px;
    border: 1px solid #9cb3bf;
  }
}
.div-chart {
  display: block;
  padding: 10px 14px 0;
}
.div-record {
  padding: 20px 14px 80px;
  position: relative;
  &-title {
    display: flex;
    position: relative;
    font-weight: 500;
    font-size: 18px;
    color: #e5f0f4;
    line-height: 25px;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 18px;
  }
  :deep(.van-tabs__wrap) {
    margin-top: 10px;
    margin-bottom: 10px;
  }
  :deep(.van-tabs__nav) {
    font-size: 14px;
    color: #606374;
    background-color: #171826;
  }
  :deep(.van-tab--active) {
    font-weight: normal;
    color: #fff;
    background-color: #0066ff;
  }
  :deep(.van-tabs__line) {
    display: none;
  }
  :deep(.van-tab) {
    flex: none;
    width: 65px;
    height: 32px;
  }
  .div-list-empty {
    padding: 40px 10px;
    background: #27283e;
    border-radius: 20px;
    border: 1px solid #62737f;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    p {
      font-size: 16px;
      color: #606374;
    }
  }
  .div-list {
    &-item {
      margin-bottom: 18px;
      padding: 20px 22px;
      background: linear-gradient(
        135deg,
        rgba(253, 253, 253, 0.18) 0%,
        rgba(253, 253, 253, 0.18) 0%,
        rgba(243, 243, 243, 0) 100%
      );
      border-radius: 20px;
      border: 1px solid #62737f;
      position: relative;
      .div-top {
        font-size: 16px;
        font-weight: bold;
        .div-status {
          color: #ffffff;
        }
        .div-success {
          color: #47ddc2;
        }
        .div-cancel {
          color: #ff6393;
        }
      }
      .div-price {
        padding-bottom: 10px;
        margin-top: 25px;
        font-size: 13px;
        color: #e5f0f4;
        position: relative;
        text-indent: 14px;
        &:before {
          position: absolute;
          content: "";
          width: 8px;
          height: 8px;
          left: 0;
          bottom: 13px;
          border-radius: 50%;
          background-color: #62e3c1;
        }
      }
      .div-amount {
        font-weight: 500;
        font-size: 21px;
        color: #e5f0f4;
        line-height: 30px;
        padding-bottom: 14px;
      }
      .pay-detail {
        font-size: 13px;
        color: #fff;
        line-height: 24px;
        .sub-desc {
          color: #62737f;
        }
      }
      .div-btn {
        position: absolute;
        right: 22px;
        bottom: 22px;
        padding: 10px 26px;
        border-radius: 8px;
        font-size: 14px;
        color: #151d24;
        font-weight: bold;
        background: radial-gradient(
          132% 97% at 11% 76%,
          #c172ff 0%,
          #c172ff 0%,
          #08e6f8 100%
        );
      }
    }
  }
}
.div-operate {
  height: 80px;
  position: fixed;
  bottom: 10px;
  left: 0;
  width: 100%;
  .div-content {
    height: 80px;
    background: linear-gradient(
      224deg,
      rgba(255, 255, 255, 0.12) 0%,
      rgba(255, 255, 255, 0.01) 92%,
      rgba(255, 255, 255, 0.01) 100%
    );
    border-radius: 20px;
    border: 1px solid #9cb3bf;
    width: calc(100% - 28px);
    max-width: 720px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-around;
    .div-btn {
      border-radius: 8px;
      font-size: 16px;
      font-weight: bold;
      padding: 10px 32px;
      &.btn-create {
        background: #47ddc2;
      }
      &.btn-order {
        background: #ff6393;
      }
    }
  }
}
</style>
